package com.bulletelc.apollopre.module.display.product.details.comment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.CommentBean;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;
import com.bulletelc.apollopre.widget.CustomGridView;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by CX on 2018/1/26.
 */

public class CommentsListAdapter extends BaseAdapter {

    private List<CommentBean> commentsList;
    private LayoutInflater inflater;
    private Context context;

    public CommentsListAdapter(Context context, List<CommentBean> commentsList) {
        this.commentsList = commentsList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return commentsList.size();
    }

    @Override
    public Object getItem(int i) {
        return commentsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        CommentBean comment = commentsList.get(i);
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.comment_list_item, viewGroup, false);
            holder.civAvatar = view.findViewById(R.id.civ_avatar);
            holder.tvUserName = view.findViewById(R.id.tv_user_name);
            holder.tvSkuName = view.findViewById(R.id.tvSkuName);
            holder.tvContent = view.findViewById(R.id.tv_content);
            holder.tvDate = view.findViewById(R.id.tv_date);
            holder.rbStart = view.findViewById(R.id.rb_start);
            holder.gvImages = view.findViewById(R.id.gv_images);

            holder.imagesAdapter = new ImagesAdapter(context, new ArrayList<String>());
            holder.gvImages.setAdapter(holder.imagesAdapter);

            view.setTag(R.id.tag_comment_show_lv, holder);
            AutoUtils.autoSize(view);//适配屏幕
        } else {
            holder = (ViewHolder) view.getTag(R.id.tag_comment_show_lv);
        }

        double mark ;
        if (comment.getTotal_mark().equals("null") ) {
            mark=0;
        } else {
            mark = Double.parseDouble(comment.getTotal_mark());
        }
        if (!"".equals(comment.getAvatar()) && comment.getAvatar() != null) {
            ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(holder.civAvatar, comment.getAvatar()));
        } else {
            holder.civAvatar.setImageResource(R.drawable.ic_default_avatar);
        }
        if (comment.getIs_anonymous() == 1) {
            holder.tvUserName.setText(comment.getUsername().substring(0,1)+"**");
        } else {
            holder.tvUserName.setText(comment.getUsername());
        }
        holder.tvSkuName.setText(comment.getProduct_sku_name());
        holder.rbStart.setRating((int) mark);
        holder.tvContent.setText(comment.getContent());
        holder.tvDate.setText(comment.getCreated_at());

        List<String> photos = comment.getPhotos();
        if (photos != null && photos.size() > 0) {
            holder.imagesAdapter.setImgPaths(photos);
            holder.imagesAdapter.notifyDataSetChanged();
        } else {
            holder.imagesAdapter.setImgPaths(new ArrayList<String>());
            holder.imagesAdapter.notifyDataSetChanged();
        }

        return view;
    }

    class ViewHolder {
        TextView tvUserName, tvSkuName,tvContent, tvDate, tvSku;
        RatingBar rbStart;
        CircleImageView civAvatar;
        CustomGridView gvImages;
        ImagesAdapter imagesAdapter;
    }

}
